
$(function(){
    var obj={
        type_ids:{},
        type_deviceId_sort:{0:{},1:{},2:{}},
        init:function(){
            // debugger;
            this.initData();
            this.eventInt();
        },
        initData:function(){
            var _this=this;
            if(_rs!=""){
                //
                $.each(_rs,function (index,obj) {
                    var deviceType=obj.device_type;
                    var deviceId=obj.device_id_;
                    var sort=obj.sort;
                    if(_this.type_ids[deviceType]==null){
                        _this.type_ids[deviceType]=deviceId;
                    }else{
                        _this.type_ids[deviceType]=_this.type_ids[deviceType]+","+deviceId;
                    }
                    // if(_this.type_deviceId_sort[deviceType]==null){
                    //     _this.type_deviceId_sort[deviceType]={}
                    // }
                    _this.type_deviceId_sort[deviceType][deviceId]=sort;
                })
            }

            //初始化标准物质
            var ids0=_this.type_ids[0];
            if(ids0!=null && ids0!=""){
                this.getSelectedBzwz(ids0);
            }

            //初始计量标准装置
            var ids1=_this.type_ids[1];
            if(ids1!=null && ids1!=""){
                this.getSelectedJlbzzz(ids1);
            }

            //初始计量标准装置
            var ids2=_this.type_ids[2];
            if(ids2!=null && ids2!=""){
                this.getSelectedJlzz(ids2);
            }
        },
        eventInt:function(){
            var _this=this;
            $("#addBzwzId").click(function(){
                parent.parent.openWindow("选择标准物质",1000,400,_root+"/standardMatter/init?menuId=sel_search_btn",[
                    {
                        text:'确认',
                        iconCls:'icon-ok',
                        handler:function(){
                            var $ecForm = parent.$("#winFrame").contents().find("#listFrame").contents().find("#ec");
                            var ids="";
                            $ecForm.find("input[type='checkbox']").each(function () {
                                if($(this).is(":checked")){
                                    if($("#bzwzTbodyId").find("input[name='bzwzMatterId'][value='"+$(this).val()+"']").size()==0)
                                    ids+=","+$(this).val();
                                }
                            })
                            if(ids!=""){
                                ids=ids.substring(1);
                                _this.getSelectedBzwz(ids);
                            }

                           parent.closeWindow();
                        }
                    },
                    {
                        text:'取消',
                        iconCls:'icon-no',
                        handler:function(){
                            parent.closeWindow();
                        }
                    }
                ],false);
            });

            $("#bzwzTbodyId").delegate('div.btn-del', 'click', function(){
                $(this).parents('tr').remove();
            });



            $("#addJlbzzzId").click(function(){
                parent.parent.openWindow("选择计量标准装置",1000,400,_root+"/standardDevice/init?menuId=sel_search_btn",[
                    {
                        text:'确认',
                        iconCls:'icon-ok',
                        handler:function(){
                            var $ecForm = parent.$("#winFrame").contents().find("#listFrame").contents().find("#ec");
                            var ids="";
                            $ecForm.find("input[type='checkbox']").each(function () {
                                if($(this).is(":checked")){
                                    if($("#jlbzzzTbodyId").find("input[name='jlbzzzDeviceId'][value='"+$(this).val()+"']").size()==0)
                                        ids+=","+$(this).val();
                                }
                            })
                            if(ids!=""){
                                ids=ids.substring(1);
                                _this.getSelectedJlbzzz(ids);
                            }

                            parent.closeWindow();
                        }
                    },
                    {
                        text:'取消',
                        iconCls:'icon-no',
                        handler:function(){
                            parent.closeWindow();
                        }
                    }
                ],false);
            });

            $("#jlbzzzTbodyId").delegate('div.btn-del', 'click', function(){
                $(this).parents('tr').remove();
            });




            $("#addJlzzId").click(function(){
                parent.parent.openWindow("选择计量装置",1000,400,_root+"/equipment/init?menuId=sel_search_btn",[
                    {
                        text:'确认',
                        iconCls:'icon-ok',
                        handler:function(){
                            var $ecForm = parent.$("#winFrame").contents().find("#listFrame").contents().find("#ec");
                            var ids="";
                            $ecForm.find("input[type='checkbox']").each(function () {
                                if($(this).is(":checked")){
                                    if($("#jlzzTbodyId").find("input[name='jlbzzzDeviceId'][value='"+$(this).val()+"']").size()==0)
                                        ids+=","+$(this).val();
                                }
                            })
                            if(ids!=""){
                                ids=ids.substring(1);
                                _this.getSelectedJlzz(ids);
                            }

                            parent.closeWindow();
                        }
                    },
                    {
                        text:'取消',
                        iconCls:'icon-no',
                        handler:function(){
                            parent.closeWindow();
                        }
                    }
                ],false);
            });



            //tab toggle
            $(document).delegate('.tab-header li', 'click', function(){
                if(!$(this).hasClass('.s0')){
                    var $index = $(this).index();
                    $(this).siblings('.s0').toggleClass('s0').end().toggleClass('s0');
                    $('div.tab-body').find('.s0').toggleClass('s0').end().find('.tab-content:eq('+$index+')').toggleClass('s0');
                }
            });

        },
        getSelectedBzwz:function(ids){
            var _this=this;
            $.ajax({
                type: 'POST',
                headers: headers,
                url: _root+"/standardMeasureScope/selectBzwz" ,
                data:{"standardMatterIds":ids},
                dataType: "json",
                success: function (data) {
                    // $("#bzwzTbodyId").html("");
                    var bzwzTbodyIdStr=$("#bzwzTbodyId").html();
                    for(var i=0;i<data.length;i++){
                        bzwzTbodyIdStr+="<tr>";
                        bzwzTbodyIdStr+="<td><input type='hidden' name='bzwzMatterId' value='"+data[i]["matter_id"]+"' /></td>"
                        bzwzTbodyIdStr+="<td>"+data[i]["matter_name"]+"</td>"
                        bzwzTbodyIdStr+="<td>"+data[i]["measure_scope"]+"</td>"
                       // bzwzTbodyIdStr+="<td><img width='100px' height='20px' src='"+_root+"/measureunitpng/"+data[i]["measure_unit"]+"' /></td>"
                        bzwzTbodyIdStr+="<td>"+data[i]["measure_unit"]+"</td>"
                        bzwzTbodyIdStr+="<td>"+data[i]["memo"]+"</td>"
                        bzwzTbodyIdStr+="<td>"+data[i]["cert_code"]+"</td>"
                        bzwzTbodyIdStr+="<td>"+data[i]["end_date"]+"</td>"

                        var diffDay=data[i]["diff_day"];
                        var diffStr="<span style='color: green;'>使用中</span>";
                        if(diffDay>=0 && diffDay<30){
                            diffStr="<span style='color: red;'>剩余"+diffDay+"天</span>";
                        }else if(diffDay < 0){
                            diffStr="<span style='color: gray;'>已过期</span>";
                        }

                        bzwzTbodyIdStr+="<td>"+diffStr+"</td>"

                        var sort=0;
                        if(_this.type_deviceId_sort[0][data[i]["matter_id"]]!=null){
                            sort= _this.type_deviceId_sort[0][data[i]["matter_id"]];
                        }

                        bzwzTbodyIdStr+="<td><input type='text' name='bzwzSort' value='"+sort+"' /></td>"
                        bzwzTbodyIdStr+="<td ><div class='btn-del' title='移除标准物质'></div></td>"
                        bzwzTbodyIdStr+="</tr>";
                    }
                    $("#bzwzTbodyId").html(bzwzTbodyIdStr);
                },
                error:function(){

                }

            });
        },
        getSelectedJlbzzz:function(ids){
            var _this=this;
            $.ajax({
                type: 'POST',
                headers: headers,
                url: _root+"/standardMeasureScope/selectJlbzzz" ,
                data:{"deviceIds":ids},
                dataType: "json",
                success: function (data) {
                    // $("#bzwzTbodyId").html("");
                    var tbodyIdStr=$("#jlbzzzTbodyId").html();
                    for(var i=0;i<data.length;i++){
                        tbodyIdStr+="<tr>";
                        tbodyIdStr+="<td><input type='hidden' name='jlbzzzDeviceId' value='"+data[i]["device_id"]+"' /></td>"
                        tbodyIdStr+="<td>"+data[i]["device_name"]+"</td>"
                        tbodyIdStr+="<td>"+data[i]["measure_scope"]+"</td>"
                        tbodyIdStr+="<td>"+data[i]["memo"]+"</td>"
                        tbodyIdStr+="<td>"+data[i]["cert_code"]+"</td>"
                        tbodyIdStr+="<td>"+data[i]["end_date"]+"</td>"

                        var diffDay=data[i]["diff_day"];
                        var diffStr="<span style='color: green;'>使用中</span>";
                        if(diffDay>=0 && diffDay<30){
                            diffStr="<span style='color: red;'>剩余"+diffStr+"天</span>";
                        }else if(diffDay < 0){
                            diffStr="<span style='color: gray;'>已过期</span>";
                        }

                        tbodyIdStr+="<td>"+diffStr+"</td>"

                        var sort=0;
                        if(_this.type_deviceId_sort[1][data[i]["matter_id"]]!=null){
                            sort= _this.type_deviceId_sort[1][data[i]["matter_id"]];
                        }

                        tbodyIdStr+="<td><input type='text' name='jlbzzzSort' value='"+sort+"' /></td>"
                        tbodyIdStr+="<td ><div class='btn-del' title='移除计量标准装置'></div></td>"
                        tbodyIdStr+="</tr>";
                    }
                    $("#jlbzzzTbodyId").html(tbodyIdStr);
                },
                error:function(){

                }

            });
        },
        getSelectedJlzz:function(ids){
            var _this=this;
            $.ajax({
                type: 'POST',
                headers: headers,
                url: _root+"/standardMeasureScope/selectJlzz" ,
                data:{"equIds":ids},
                dataType: "json",
                success: function (data) {
                    // $("#bzwzTbodyId").html("");
                    var tbodyIdStr=$("#jlzzTbodyId").html();
                    for(var i=0;i<data.length;i++){
                        tbodyIdStr+="<tr>";
                        tbodyIdStr+="<td><input type='hidden' name='jlzzDeviceId' value='"+data[i]["equ_id"]+"' /></td>"
                        tbodyIdStr+="<td>"+data[i]["equ_name"]+"</td>"
                        tbodyIdStr+="<td>"+data[i]["measure_scope"]+"</td>"
                        tbodyIdStr+="<td>"+data[i]["equ_memo"]+"</td>"
                        tbodyIdStr+="<td>"+data[i]["cert_code"]+"</td>"
                        tbodyIdStr+="<td>"+data[i]["next_equ_date"]+"</td>"

                        var diffDay=data[i]["diff_day"];
                        var diffStr="<span style='color: green;'>使用中</span>";
                        if(diffDay>=0 && diffDay<30){
                            diffStr="<span style='color: red;'>剩余"+diffStr+"天</span>";
                        }else if(diffDay < 0){
                            diffStr="<span style='color: gray;'>已过期</span>";
                        }

                        tbodyIdStr+="<td>"+diffStr+"</td>"

                        var sort=0;
                        if(_this.type_deviceId_sort[2][data[i]["matter_id"]]!=null){
                            sort= _this.type_deviceId_sort[2][data[i]["matter_id"]];
                        }

                        tbodyIdStr+="<td><input type='text' name='jlzzSort' value='"+sort+"' /></td>"
                        tbodyIdStr+="<td ><div class='btn-del' title='移除计量装置'></div></td>"
                        tbodyIdStr+="</tr>";
                    }
                    $("#jlzzTbodyId").html(tbodyIdStr);
                },
                error:function(){

                }

            });
        }
    }
    obj.init();
})